﻿jQuery(document).ready(function () {

    leftString = false;
    // if IE7, change left to 25%
    if (jQuery(".lt-ie8").length > 0) {
        leftString = "25%";
    }

    jQuery('.iframe').colorbox({
        iframe: true,
        scrolling: false,
        transition: "fade",
        height: 500,
        innerWidth: 700,
        maxWidth: 702,
        left: leftString,
        top: "20%",
        opacity: 0.6
    });

    // For culture videos
    jQuery('.applynowbtn.iframe').colorbox({
        iframe: true,
        scrolling: false,
        transition: "fade",
        height: 580,
        innerWidth: 700,
        maxWidth: 702,
        left: leftString,
        opacity: 0.6
    });

    jQuery('.inlineColorbox').colorbox({
        inline: true,
        transition: "fade",
        left: leftString,
        height: 540,
        top: "20%",
        opacity: 0.6
    });

    jQuery('.inlineModal  a.closeModal').click(function () { jQuery.colorbox.close(); })

    // Pressing enter saves the search name
    jQuery('.searchNameInput').bind('keyup', function (e) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == 13) {
            (jQuery(this).parent().siblings('.savedSearchEditControls')).find('a:first').click();
        }
    });

    // When clicking checkboxes in the modal popup
    jQuery('.inlineModal input[type=checkbox]').change(function () {
        modalCheckChange(this);
    });

    // When clicking checkboxes in the sidebar
    jQuery('.sidebarbox table input[type=checkbox]').change(function () {
        sidebarCheckChange(this);
    })

    // Remove underline for last nav item in each column
    /*
    jQuery('.navigationlinkLiUl').each(function () {
    (jQuery(this).find('li').last()).css('border', 'none');
    });
    */

    // Remove underline for last section of content
    jQuery('#ColumnL section:last').css('border', 'none');

    // Set the 'Enter' key to submit forms
    jQuery(':input').bind('keypress', function (e) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == 13) {
            e.preventDefault();
            var submit = jQuery(this).parent().find('input[type=submit]');
            jQuery(submit).click();
        }
    });
});

var atLeastOneChecked = false;

function clearcheckboxes(id, elementId) {
    var element = document.getElementById(id);
    var checkboxes = element.getElementsByTagName('input');

    // If clicking 'All'
    if (elementId.endsWith('_0')) {
        for (var i = 1; i < checkboxes.length; i++) {
            checkboxes[i].checked = false;
        }
    }
    else {
        checkboxes[0].checked = false;
    }

    for (var i = 1; i < checkboxes.length; i++) {
        if (checkboxes[i].checked) {
            atLeastOneChecked = true;
        }
        // Call on last loop
        if (i == checkboxes.length - 1) {
            if (!atLeastOneChecked) {
                checkboxes[0].checked = true;
            }
        }
    }
}

function modalCheckChange(obj) {
    atLeastOneChecked = false;
    var isChecked = obj.checked;
    var idArray = obj.id.split("_");
    var idStartLong = idArray[0] + "_" + idArray[1] + "_" + idArray[2];
    var idStart = idStartLong.substr(0, idStartLong.length - 1);
    idStart = idStart.replace("More", "");

    // Needs to account for larger than 10
    var idNum = obj.id.substr(obj.id.length - 2);
    idNum = idNum.replace("_", "");
    jQuery('#' + idStart + '_' + idNum).attr('checked', isChecked);
    clearcheckboxes(idStartLong, obj.id);
    clearcheckboxes(idStart, obj.id);
}

function sidebarCheckChange(obj) {
    atLeastOneChecked = false;
    var isChecked = obj.checked;
    var idNum = obj.id.substr(obj.id.length - 1);
    var idStartLong = obj.id.substr(0, obj.id.length - 2);
    var checkField = obj.id.substr(0, obj.id.length - 2);
    checkField = checkField.substr(11, checkField.length);
    var modalChecks = jQuery('.inlineModal [id*=' + checkField + ']');
    (jQuery(modalChecks).find('input:checkbox:first')).attr('checked', false);
    // If checking 'All'
    if (idNum == 0) {
        jQuery(modalChecks).each(function () { jQuery(this).attr('checked', false); })
    }
    var targetCheckbox = jQuery(modalChecks).find('[id$=_' + idNum + ']');
    jQuery(targetCheckbox).attr('checked', isChecked);
    clearcheckboxes(idStartLong, obj.id);
}

function viewJobs() {
    parent.jQuery.colorbox.close();
    window.location = '/';
}

function editFinished(obj, save) {
    var savedRow = jQuery(obj).parents('.savedSearchRow');
    var searchName = jQuery(savedRow).find('a.searchName');
    var searchInput = jQuery(savedRow).find('.searchNameInput');

    // If 'save' is true, save the change, otherwise ignore it
    if (save) {
        jQuery(searchName).text(jQuery(searchInput).val());
        updatedSavedList();
    }

    // Show and hide appropriate controls
    jQuery(obj).parent().hide();
    jQuery(savedRow).find('.savedSearchControls').show();

    jQuery(searchInput).hide();
    jQuery(searchName).show();
    jQuery(savedRow).toggleClass('edit');
}

function expandCollapse(obj) {
    jQuery(obj).toggleClass('open');
    var content = jQuery(obj).siblings('.sidebarContent');
    content.toggle();
}

function deleteSavedSearch(obj, showdiv, hidediv) {
    jQuery(obj).parents('.savedSearchRow').fadeOut(function () {
        var savedRow = jQuery(obj).parents('.savedSearchRow');
        var searchName = jQuery(savedRow).find('a.searchName');
        var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
        var date = -1;
        if (isChrome) {
            date = new Date(0).toUTCString();
        }
        jQuery.cookie('savedsearch-' + jQuery(searchName).text(), null, { expires: date, path: "/" });
        jQuery(this).remove();
        jQuery('.searchAmt').html(jQuery('.savedSearchRow').length);

        updatedSavedList();
        var savedSearchCount = document.getElementById('currentsavedsearches');
        try {

            var currentSavedCount = parseInt(savedSearchCount.innerHTML);
            currentSavedCount -= 1;
            savedSearchCount.innerHTML = currentSavedCount;

            if (document.getElementById('savedSearchCount') != null)
                document.getElementById('savedSearchCount').innerHTML = currentSavedCount;

            if (currentSavedCount == 0) {
                document.getElementById('numberofsavedsearches').innerHTML = 'No Saved Searches';
                document.getElementById(showdiv).style.display = 'block';
                document.getElementById(hidediv).style.display = 'none';
                jQuery('.emailSearch').hide();
            }
        } catch (e) {
            savedSearchCount.value = '';
        }
    });
}

function editSavedSearch(obj) {
    var savedRow = jQuery(obj).parents('.savedSearchRow');
    // Show and hide appropriate controls
    jQuery(obj).parent().hide();
    jQuery(savedRow).find('.savedSearchEditControls').show();

    var searchName = jQuery(savedRow).find('a.searchName');
    var searchInput = jQuery(savedRow).find('.searchNameInput');

    jQuery(searchName).hide();
    jQuery(searchInput).val(searchName.text());
    jQuery(searchInput).show().focus();

    jQuery(savedRow).toggleClass('edit');
}

function editFinished(obj, save) {
    var savedRow = jQuery(obj).parents('.savedSearchRow');
    var searchName = jQuery(savedRow).find('a.searchName');
    var searchInput = jQuery(savedRow).find('.searchNameInput');

    // If 'save' is true, save the change, otherwise ignore it
    if (save) {

        var today = new Date();
        var day = today.getDay();
        var month = today.getMonth();
        var year = today.getFullYear();

        var oldCookie = jQuery.cookie('savedsearch-' + jQuery(searchName).text());
        var url = oldCookie.split('|')[0];
        var newcookieValue = url + "|" + month + '/' + day + '/' + year;

        var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
        var date = -1;
        if (isChrome) {
            date = new Date(0).toUTCString();
        }

        jQuery.cookie('savedsearch-' + jQuery(searchName).text(), null, { expires: date, path: "/" });
        jQuery.cookie('savedsearch-' + jQuery(searchInput).val(), newcookieValue, { expires: 60, path: "/" });
        jQuery(searchName).text(jQuery(searchInput).val());
        updatedSavedList();
    }

    // Show and hide appropriate controls
    jQuery(obj).parent().hide();
    jQuery(savedRow).find('.savedSearchControls').show();

    jQuery(searchInput).hide();
    jQuery(searchName).show();
    jQuery(savedRow).toggleClass('edit');
}

// Updates search names in the email modal
function updatedSavedList() {
    var modalSearchNames = "";
    var savedSearches = jQuery('a.searchName');
    jQuery(savedSearches).each(function () {
        modalSearchNames += '<input class="checkbox" checked type="checkbox"><label class="searchName">' + jQuery(this).text() + '</label><br />';
    });
    jQuery('#emailSearch .contentBlock:first').html(modalSearchNames);
}



// Updates search names in the email modal
function updatedSavedList() {
    var modalSearchNames = "";
    var savedSearches = jQuery('a.searchName');
    jQuery(savedSearches).each(function () {
        modalSearchNames += '<input class="checkbox" checked type="checkbox"><label class="searchName">' + jQuery(this).text() + '</label><br />';
    });
    jQuery('#emailSearch .contentBlock:first').html(modalSearchNames);
}




function savejob(jobId) {
    if (jobId != '') {

        var savedJobs = jQuery.cookie("mysavedjobs") == null ? '' : jQuery.cookie("mysavedjobs");

        var tempMyJobs = savedJobs.split(',');

        var notInMyJobs = true;
        for (var i = 0; i < tempMyJobs.length; i++) {
            notInMyJobs = tempMyJobs[i] == jobId ? false : notInMyJobs;
        }

        if (notInMyJobs) {
            savedJobs += notInMyJobs ? jobId + ',' : savedJobs;
            jQuery.cookie("mysavedjobs", savedJobs, { expires: 60, path: "/" });
            try {
                var jobContainer = document.getElementById('jobcontainer_' + jobId);
                jobContainer.innerHTML = '<img src=\'/~/Media/Careers20/Images/Icons/iconFolderCheck.ashx\' alt=\'Saved Job\' />' + jobContainer.innerHTML;
                var quicklookmodaldiv = document.getElementById('quicklookHeading_' + jobId);
                quicklookmodaldiv.innerHTML = '<img src=\'/~/Media/Careers20/Images/Icons/iconFolderCheck.ashx\' alt=\'Saved Job\' />' + quicklookmodaldiv.innerHTML;
            } catch (e) {

            }

            var savedJobsLink = document.getElementById('savedJobsLink');
            if (savedJobsLink) {
                savedJobsLink.innerHTML = 'View saved jobs';
                var savedJobsCount = document.getElementById('savedJobsCount');
                try {
                    var currentCount = parseInt(savedJobsCount.innerHTML);
                    currentCount += 1;
                    savedJobsCount.innerHTML = currentCount;
                } catch (e) {
                    savedJobsCount.value = '';
                }
            }
        }
    }
    return false;
}

function deletejob(jobId, showdiv, hidediv) {
    if (jQuery.cookie("mysavedjobs") != null) {
        var myJobs = jQuery.cookie("mysavedjobs").toString().replace(jobId + ',', '');
        jQuery.cookie("mysavedjobs", myJobs, { expires: 60, path: "/" });

        jQuery('#jobcontainer_' + jobId).fadeOut();
        //document.getElementById('jobcontainer_' + jobId).style.display = 'none';

        if (document.getElementById('savedJobsLink')) {
            var savedJobsLink = document.getElementById('savedJobsLink');
            savedJobsLink.innerHTML = 'View saved jobs';
        }
        var savedJobsCount = document.getElementById('currentsavedjobs');
        //var savedJobsCount1 = document.getElementById('savedJobsCount');
        try {
            var currentCount = parseInt(savedJobsCount.innerHTML);
            currentCount -= 1;
            savedJobsCount.innerHTML = currentCount;
            //savedJobsCount1.innerHTML = currentCount;
            if (currentCount == 0) {
                if (savedJobsLink) {
                    savedJobsLink.innerHTML = 'How to save a Job';
                }
                document.getElementById(showdiv).style.display = 'block';
                document.getElementById(hidediv).style.display = 'none';
                jQuery('.emailJobs').hide();
                document.getElementById('numberofsavedjobs').innerHTML = 'No Saved Jobs';
            }
        } catch (e) {
            savedJobsCount.value = '';
            //savedJobsCount1.value = '';
        }


    }
}


function expander(obj, e) {
    var span = obj.getElementsByTagName('span')[0];
    switch (e.type) {
        case "mouseover":
            span.innerHTML = '- ';
            break;
        case 'mouseout':
            span.innerHTML = '+ ';
            break;
    }
}

function cleartext(obj, defaultText) {
    if (obj.value.toLowerCase() == defaultText.toLowerCase()) {
        obj.value = '';
    }
}

function addtext(obj, text) {
    if (obj.value == '') {
        obj.value = text;
    }
}

function jobquicklookmouseover(contestnumber) {
    document.getElementById('quicklookmodal_' + contestnumber).style.display = 'block';
    if (document.getElementById('jobdescription' + contestnumber).innerHTML.toString().replaceAll(' ', '') == 'loading...') {
        getjobdescription(contestnumber);
    }
}
function jobquicklookmouseout(contestnumber) {
    document.getElementById('quicklookmodal_' + contestnumber).style.display = 'none';
}

var xmlHttpRequest;
function getjobdescription(contestnumber) {
    if (xmlHttpRequest != null) {
        xmlHttpRequest.abort();
        xmlHttpRequest = null;
    }
    if (window.XMLHttpRequest)// code for Firefox, Opera, IE7, etc.
    {
        xmlHttpRequest = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)// code for IE6, IE5
    {
        xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlHttpRequest != null) {
        xmlHttpRequest.onreadystatechange = function () { try { if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) { document.getElementById('jobdescription' + contestnumber).innerHTML = xmlHttpRequest.responseText; } } catch (error) { } };
        xmlHttpRequest.open("POST", "/UhgErp/Services/JobInfo.ashx?get=jobdescription&contestnumber=" + contestnumber, true);
        xmlHttpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xmlHttpRequest.setRequestHeader('Contgent-length', 0);
        xmlHttpRequest.setRequestHeader('Connection', 'close');
        xmlHttpRequest.setRequestHeader("If-Modified-Since", new Date(0));
        xmlHttpRequest.send(null);
    }
    else {
        alert("Your browser does not support XMLHTTP.");
    }

    return false;
}

function redirectToUrl(url, extenstion) {
    if (url.indexOf('?') > -1 && extenstion != '') {
        url += '&' + extenstion;
    }
    else if (extenstion != '') {
        url += '?' + extenstion;
    }
    window.location.href = url;
}

String.prototype.endsWith = function (inText) {
    return this.indexOf(inText, this.length - inText.length) !== -1;
};

String.prototype.replaceAll = function (whatToreplace, replacewith) {
    var str = this;
    if (whatToreplace == ' ') {
        str = str.replace(/\s+/g, replacewith);
    }
    else {
        str = str.replace(/whatToreplace/g, replacewith);
    }

    return str;
}

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+jQuery/g, "");
}
String.prototype.ltrim = function () {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function () {
    return this.replace(/\s+jQuery/, "");
}

function faqToggle(obj) {
    jQuery(obj).parents('.faqEntry').toggleClass('open');
}

function expandFaqs() {
    jQuery(".faqEntry:visible").addClass('open');
}

function collapseFaqs() {
    jQuery(".faqEntry:visible").removeClass('open');
}

function tabClick(obj) {
    var id = jQuery('.tabRow .tab').index(jQuery(obj));
    jQuery('.tabRow .tab').removeClass('on');
    jQuery(obj).addClass('on');
    jQuery('.tabContent').hide();
    jQuery('.tabContent').eq(id).show();
}

function showChat() {
    $('.chatHover').show();
}

function hideChat() {
    $('.chatHover').hide();
}

function addiframe(id, url, height, width) {
    url = url.replace(/&amp;/g, '%26');
    var ifrm = document.createElement('iframe');
    ifrm.setAttribute('src', unescape(url));
    ifrm.setAttribute('scrolling', 'no');
    ifrm.setAttribute('frameborder', '0');
    ifrm.setAttribute('marginwidth', '0');
    ifrm.setAttribute('marginheight', '0');
    //ifrm.setAttribute('onload', 'javascript:changeWrapper();');
    ifrm.style.width = width;
    ifrm.style.height = height;
    document.getElementById(id).appendChild(ifrm);
    fixie(id);
}

function fixie(iframeid) {
    try {
        var ie7 = navigator.appVersion.indexOf('MSIE 7.') > -1;
        if (ie7) {
            var iframe = document.getElementById(iframeid).childNodes[0];
            iframe.style.width = (iframe.offsetWidth + 20) + 'px';
            iframe.style.height = (iframe.offsetHeight + 20) + 'px';
        }
    }
    catch (error) {

    }
}

function googlegoals(url, goal) {
    if (url.indexOf('?') > -1) {
        url += '&' + goal;
    }
    else {
        url += '?' + goal;
    }
    window._gaq.push(['_trackPageview', url]);
}